﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;

namespace ProjectCloth.FrmUI.ColorCards
{
    public partial class ColorCardFrms : UserControl
    {
        private ClothContext db = new ClothContext();
        //private bool isSave = false;
        public ColorCardFrms()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 0 && (dataGridView1.Rows[e.RowIndex].Cells["ColorCardNo"] == null || dataGridView1.Rows[e.RowIndex].Cells["ColorCardNo"].Value == null))
            {
                var form = new ProductSelectFrm();
                var result = form.ShowDialog();
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = form.productNo;
                SendKeys.Send("{Tab}");
            }
        }
        //改变键盘键enter效果
        #region
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            base.ProcessCmdKey(ref msg, keyData);
            var dgv = dataGridView1;
            bool _disable = false;

            switch (keyData)
            {
                case Keys.Enter:
                    if (dgv.CurrentRow != null)
                    {
                        SendKeys.Send("{Tab}");
                    }


                    _disable = true;
                    break;
                //case Keys.F12:
                //    {
                //        isSave = true;
                //        SendKeys.Send("{Tab}");
                //        saveBtn_Click(null, null);
                //        _disable = true;
                //        break;
                //    }
                default:
                    break;
            }

            return _disable;
        }
        #endregion
        private void saveBtn_Click(object sender, EventArgs e)
        {
            var dgv = dataGridView1;
            //isSave = true;
            //SendKeys.Send("{Tab}");
            List<ColorCard> list = new List<ColorCard>();
            for (int i = 0; i < dgv.RowCount - 1; i++)
            {
                if (dgv.Rows[i].Cells["ColorCardNo"] != null && dgv.Rows[i].Cells["ColorCardNo"].Value != null
                    //&& dgv.Rows[i].Cells["Quantity"] != null && dgv.Rows[i].Cells["Quantity"] != null
                    )
                {

                    ColorCard cc = new ColorCard()
                    {
                        ColorCardNo = dgv.Rows[i].Cells["ColorCardNo"].Value.ToString(),
                        Quantity = Convert.ToDecimal(dgv.Rows[i].Cells["Quantity"].Value),//数量                                                                                   
                        CreateTime = DateTime.Now,
                        MakeTime = createTimePick.Value,
                        Enabled = true
                    };
                    list.Add(cc);
                }

            }
            db.ColorCards.AddRange(list);
            var itemsResult = db.SaveChanges();
            if (itemsResult > 0)
            {
                MessageBox.Show("保存成功！");
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
            createTimePick.Value = createTimePick.Value.AddDays(1);
            dgv.Rows.Clear();
            //isSave = false;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.ColumnIndex == 0)
            //{
            //    var form = new ProductSelectFrm();
            //    var result = form.ShowDialog();
            //    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = form.productNo;
            //}

        }
    }
}
